package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.fixedActionService;
import cn.itcast.bos.utils.BaseAction;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class fixedArea extends BaseAction<FixedArea> {

	// Service 注入
	@Autowired
	private fixedActionService service;

	// 保存款地缘信息
	@Action(value = "fixedArea_save", results = {
			@Result(name = "success", location = "./pages/base/fixed_area.html", type = "redirect") })
	public String fixedSave() {

		service.save(model);

		return SUCCESS;
	}

	// 分页查询
	@Action(value = "fixdearea_Query", results = { @Result(name = "success", type = "json") })
	public String fixedQuery() {
		// 获取前台数据 构造分页查询对象
		Pageable pageable = new PageRequest(page - 1, rows);
		// 构造条件查询对象
		Specification<FixedArea> specification = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				List<Predicate> list = new ArrayList<Predicate>();

				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
					list.add(p1);
				}

				/*
				 * if (StringUtils.isNotBlank(fixedArea.getId())) { Predicate p1
				 * = cb.equal(root.get("id").as(String.class),
				 * fixedArea.getId()); list.add(p1); }
				 */
				if (StringUtils.isNotBlank(model.getCompany())) {

					Predicate p2 = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					list.add(p2);
				}

				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		// 调用业务层
		Page<FixedArea> pageArea = service.findPageData(specification, pageable);
		// 压入值栈
		pushPageDataToValueStack(pageArea);
		return SUCCESS;
	}

	// 查询未关联客户信息
	@Action(value = "fixedArea_usernofindnoassection", results = { @Result(name = "success", type = "json") })
	public String usernofindnoassection() {
		// 调用webClient调用webService 接口
		Collection<? extends Customer> collections = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/usernofindnoassection")
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collections);
		return SUCCESS;
	}

	// 查询已经关联的客户信息
	@Action(value = "fixedArea_userhasfixedassection", results = { @Result(name = "success", type = "json") })
	public String userhasfixedassection() {
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/userfixedassection/"
						+ model.getId())
				.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		// 压入值栈
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	// 属性驱动
	private String[] customerIds;

	// customerIds ----是什么？？ 是做什么用的？？ --------是关联矿中 已和定区关联的框的name属性
	// 通过他获取所有已关联框中的id
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	// 对已关联定区的客户进行提交
	@Action(value = "decidedzone_assigncustomerstodecidedzone", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String assigncustomerstodecidedzone() {

		String customerIdStr = StringUtils.join(customerIds, ",");// 是在做什么...StringUtils.join(customerIds,

		/*
		 * //取消关联 依据定区ID 去进行 客户和 定区之间的关联的取消 在客户表中 进行 定区ID的查询 如果 有 定区ID 则将其
		 * 变为null 然后在执行关联的添加 操作 WebClient .create(
		 * "http://localhost:9002/crm_management/services/customerService/assignNocustomerstodecidedzone?customerIdStr="
		 * + "fixedAreaId=" + model.getId()) .put(null); /// 为什么 put 为 null
		 */

		// 进行关联
		/// customerIdStr ---是什么？？
		WebClient
				.create("http://localhost:9002/crm_management/services/customerService/assigncustomerstodecidedzone?customerIdStr="
						+ customerIdStr + "&fixedAreaId=" + model.getId())
				.put(null); /// 为什么 put 为 null

		return SUCCESS;
	}

	// 快递员
	// 对快递员进行查询和创建与定区之间的关联
	// 查询没有和定区关联的快递员
	@Action(value = "courier_findnoassociaction", results = { @Result(name = "success", type = "json") })
	public String findnoassociaction() {
		// 调用业务层 查询未关联定区的快递员
		List<Courier> courier = service.findNoAssociation();
		// 将查询到的快递员列表压入值栈
		ActionContext.getContext().getValueStack().push(courier);
		return SUCCESS;
	}

	// 属性驱动
	private Integer courierId;
	private Integer takeTimeId;

	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}
	
	//关联哭地缘
	@Action(value="fixedArea_associationCourierToFixedArea",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String associationCourierToFixedArea(){
		//调用业务层
		service.associationCourierToFixedArea(model,courierId,takeTimeId);
		return SUCCESS;
	}

}
